import com.sun.org.apache.regexp.internal.RE;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//class A extends TestEnum{
//    public  A(){
//        super("12",12);
//    }
//}
public enum TestEnum {
//    RED,BLACK,GREEN,WHITE;//枚举对象

    //自定义枚举类，是默认继承Enum类
RED("红色",1),BLACK("黑色",2),GREEN("绿色",3),WHITE("白色",4);//枚举对象
    public String color;
    public int ordinal;
    // 枚举类的构造方法默认为私有的private
    //默认后面还有两个参数
    TestEnum(String color,int ordinal){
            this.color = color;
            this.ordinal = ordinal;
    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
    }
    // CompareTo //比较两枚举常量的顺序
    public static void main4(String[] args) {
        System.out.println(RED.compareTo(WHITE));
    }
    //valueOf方法是将字符串转换为枚举常量
    public static void main3(String[] args) {
        TestEnum testEnum = TestEnum.valueOf("RED");
        System.out.println(testEnum);
        TestEnum testEnum1 = TestEnum.valueOf("RED2");
        System.out.println(testEnum1);
    }

    //values/ordinal 方法
    public static void main2(String[] args) {
        TestEnum[] testEnums = TestEnum.values();
        for (int i = 0; i < testEnums.length; i++) {
            System.out.println(testEnums[i] + " " + testEnums[i].ordinal());
        }
    }
    public static void main1(String[] args) {
       TestEnum testEnum= TestEnum.BLACK;

       //用于swtich语句
       switch (testEnum){
           case RED:
               System.out.println("RED");
               break;
           case WHITE:
               System.out.println("WHITE");
               break;
           case GREEN:
               System.out.println("GREEN");
             break;
           case BLACK:
               System.out.println("BLACK");
               break;
           default:
               System.out.println("default");
               break;
       }
    }
    public static void reflectPrivateConstructor() {
        Class<?> c1 = null;
        try {
            //1.获取class对象
            c1 = Class.forName("TestEnum");
            //2.获取到构造方法
//            Constructor<?> constructor = c1.getDeclaredConstructor(String.class,int.class);

            Constructor<?> constructor = c1.getDeclaredConstructor(String.class,int.class,String.class,int.class);
//            TestEnum testEnum =(TestEnum) c1.newInstance();
            //3.修改权限
            constructor.setAccessible(true);

            TestEnum testEnum = (TestEnum) constructor.newInstance("haha",99,"父类的参数字符串",100);
            System.out.println(testEnum);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
//枚举是一个类型，是一个对象
//通过反射来获取一个私有的枚举对象